<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input v-model="form.username" placeholder="用户名手机"> </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input v-model="form.captcha" placeholder="验证码">
        <template slot="append">
          <el-button @click="handleSendCaptcha">
            发送验证码
          </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input v-model="form.nickname" placeholder="你的名字"> </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        v-model="form.password"
        placeholder="密码"
        type="password"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="checkPassword">
      <el-input
        v-model="form.checkPassword"
        placeholder="确认密码"
        type="password"
      >
      </el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">
      注册
    </el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPassword: ""
      },
      // 表单规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 11, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "请输入你的名字", trigger: "blur" }
        ],
        captcha: [{ min: 4, message: "4位数", trigger: "blur" }],
        password: [
          { required: true, message: "请输入用密码", trigger: "blur" }
        ],
        checkPassword: [
          {
            validator: validatePass,
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      if (!this.form.username) {
        this.$message({
          message: "请输入用户名或手机号",
          type: "info"
        });
        return;
      };
      // 获取验证码
     this.$store.dispatch('user/sendCode',this.form.username).then(code => {
          this.$confirm(`获取验证吗成功, 验证码${code}`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "success"
        });
      })

    },

    // 注册
    handleRegSubmit() {
      //  console.log(this.form)
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        }
        const { checkPassword, ...props } = this.form;
        this.$axios({
          method: "post",
          url: "/accounts/register",
          data: props
        }).then(res => {
          //  console.log(res)
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "注册成功"
            });
            this.$router.push("/user/login");
          }
        });
      });
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>
